<?php

class Usuario{
	
	public $bd;
	public $Sesion;
	public $resultado;
	public $rows;
	public $tipousuario;
	
	public function __construct(){
		
		$this->bd = new Basedatos();
		$this->Sesion = new Sesion();
		
	}
	
	public function logear(){
		
		$query = "CALL Login('".$_POST['username']."','".$_POST['password']."')";
		$this->resultado = $this->bd->select($query);
		$this->rows = mysql_num_rows($this->resultado);
		if($this->rows > 0){
			
			if($row=mysql_fetch_array($this->resultado)){
				
				$this->Sesion->iniciar();
				$this->Sesion->asignar('user', $row["usuario"]);
				$this->Sesion->asignar('name', $row["nombre_usuario"]);
				$this->Sesion->asignar('apep', $row["ap_paterno"]);
				$this->Sesion->asignar('iduser', $row["rut_usuario"]);
				$this->Sesion->asignar('idprofile', $row["id_nivelacceso"]);
				$this->Sesion->asignar('nombreAcceso', $row["nombre_acceso"]);
				$this->Sesion->asignar('habilitado', $row["habilitado"]);

			header('Location: ../panel/index.php');

		}			
		}elseif($row["id_nivelacceso"] == null){
			header('Location: ../panel/index.php?error=3');	
		}else{
			header('Location: ../panel/index.php?error=1');		
		}

		}	 /** FIN DE FUNCION LOGEAR **/

	public function smenu(){
				$ina = $_SESSION['idprofile'];
				$query = "CALL Menu(".$ina.")";
				$this->resultado = $this->bd->select($query);
				$this->rows = mysql_num_rows($this->resultado);
				if($this->rows < 1){
					echo '<h4 style="display: block;
											width: 180%;
											margin: 20px 3% 0 150%;
											margin-top: 20px;
											-webkit-border-radius: 5px;
											-moz-border-radius: 5px;
											border-radius: 5px;
											background: #F3D9D9 url(../images/icn_alert_error.png) no-repeat;
											background-position: 10px 10px;
											border: 1px solid #D20009;
											color: #7B040F;
											padding: 10px 0;
											text-indent: 40px;
											font-size: 14px;">No hay modulos asignados a su acceso, Usted sera desconectado del sistema en unos segundos</h4>';
					header('Refresh:5; url= ../log_out.php');

				}
				while($this->rows=mysql_fetch_array($this->resultado)){
							echo '<li class="icn_new_article"><a href="'.$this->rows['link_modulo'].'">'.$this->rows['nombre_modulo'].'</a></li>';
					}
	}
	public function insertar_usuario(){
		
		$query = "CALL IngresarUsuario('".$_POST["rut"]."', '".$_POST["nombre"]."', '".$_POST["ap_pat"]."', '".$_POST["ap_mat"]."', '".$_POST["fecha"]."', '".$_POST["cargo"]."')";
		$this->result = $this->bd->insert($query);
		
	}
	public function mostrar_usuario(){
		
		$query = "CALL ConsultarTodosUsuario()";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}
	public function mostrar_usu_uno($id){
		
		$query = "CALL ConsultarUnUsuario('".$id."')";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}
	public function mostrar_usu_nom($id){
		
		$query = "CALL ConsultarUsuarioNombre('".$id."')";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}

	public function mostrar_usu_ape($id){
		
		$query = "CALL ConsultarUsuarioApellidoPaterno('".$id."')";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}
	public function mostrar_habilitado($id){
		
		$query = "CALL 	ConsultarUsuarioMasHabilitado('".$id."')";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}

	public function mostrar_usu_na($id){
		$query = "CALL ConsultarUsuarioDeNivelAcceso($id)";
		$this->result = $this->bd->select($query);
		return $this->result;
	}
	public function editar_usuario($id, $nom_usuario, $ap_p, $ap_m, $fecha, $idc){
		$query = "CALL ModificarUsuario('".$id."','".$nom_usuario."','".$ap_p."','".$ap_m."','".$fecha."','".$idc."')";
		$this->result = $this->bd->update($query);
	}
	public function eliminar_usuario($id){
		$query = "CALL EliminarUsuario('".$id."')";
		$this->result = $this->bd->delete($query);
	}
	public function habilitar_usuario($id){
		$query = "CALL HabilitarUsuario('".$id."')";
		$this->result = $this->bd->update($query);
	}
	public function deshabilitar_usuario($id){
		$query = "CALL DeshabilitarUsuario('".$id."')";
		$this->result = $this->bd->update($query);
	}
	public function habilitados_usuario(){
		$query = "CALL ConsultarUsuariosHabilitados()";
		$this->result = $this->bd->select($query);
		return $this->result;
	}
	public function deshabilitados_usuario(){
		$query = "CALL ConsultarUsuariosDeshabilitados()";
		$this->result = $this->bd->select($query);
		return $this->result;
	}
		public function usuarios_enna($id){
		$query = "CALL ConsultarUsuarioDeNivelAcceso($id)";
		$this->result = $this->bd->select($query);
		return $this->result;
	}
		public function usuarios_nona($id){
		$query = "CALL ConsultarUsuarioNoConNivelAcceso($id)";
		$this->result = $this->bd->select($query);
		return $this->result;
	}

	/* ****************************************************************** Metodos de alumno en la clase usuario *********************************************************************/
	
	public function insertar_alumno(){
		
		$query = "CALL IngresarUsuario('".$_POST["rut"]."', '".$_POST["nombre"]."', '".$_POST["ap_pat"]."', '".$_POST["ap_mat"]."', '".$_POST["fecha"]."', '".$_POST["cargo"]."')";
		$this->result = $this->bd->insert($query);
	}
	public function mostrar_alumno(){
		
		$query = "CALL ConsultarTodosAlumno()";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}
	public function mostrar_alu_uno($id){
		
		$query = "CALL ConsultarUnAlumno('".$id."')";
		$this->result = $this->bd->select($query);
		return $this->result;		
		
	}
	public function editar_alumno($id, $nom_usuario, $ap_p, $ap_m, $fecha, $idc){
		$query = "CALL ModificarAlumno('".$id."','".$nom_usuario."','".$ap_p."','".$ap_m."','".$fecha."','".$idc."')";
		$this->result = $this->bd->update($query);
	}
	public function eliminar_alumno($id){
		$query = "CALL EliminarAlumno('".$id."')";
		$this->result = $this->bd->delete($query);
	}


	/********************************************************************************************************************************************************************************/

}
?>